import { defineStore } from 'pinia';
import { store } from '/@/store';
import { PageEnum } from '/@/enums/pageEnum';
import { DB_DICT_DATA_KEY, LOGIN_INFO_KEY, ROLES_KEY, TENANT_ID, TOKEN_KEY, USER_INFO_KEY } from '/@/enums/cacheEnum';
import { getAuthCache, setAuthCache } from '/@/utils/auth';
import { doLogout, getUserInfo, loginApi, phoneLoginApi, thirdLogin } from '/@/api/sys/user';
import { useI18n } from '/@/hooks/web/useI18n';
import { useMessage } from '/@/hooks/web/useMessage';
import { router } from '/@/router';
import { usePermissionStore } from '/@/store/modules/permission';
import { PAGE_NOT_FOUND_ROUTE } from '/@/router/routes/basic';
import { isArray } from '/@/utils/is';
import { useGlobSetting } from '/@/hooks/setting';
import { JDragConfigEnum } from '/@/enums/jeecgEnum';
import { useSso } from '/@/hooks/web/useSso';
export const useUserStore = defineStore({
    id: 'app-user',
    state: () => ({
        // 用户信息
        userInfo: null,
        // token
        token: undefined,
        // 角色列表
        roleList: [],
        // 字典
        dictItems: [],
        // session过期时间
        sessionTimeout: false,
        // Last fetch time
        lastUpdateTime: 0,
        //租户id
        tenantId: '',
        //登录返回信息
        loginInfo: null,
    }),
    getters: {
        getUserInfo() {
            return this.userInfo || getAuthCache(USER_INFO_KEY) || {};
        },
        getLoginInfo() {
            return this.loginInfo || getAuthCache(LOGIN_INFO_KEY) || {};
        },
        getToken() {
            return this.token || getAuthCache(TOKEN_KEY);
        },
        getAllDictItems() {
            return this.dictItems || getAuthCache(DB_DICT_DATA_KEY);
        },
        getRoleList() {
            return this.roleList.length > 0 ? this.roleList : getAuthCache(ROLES_KEY);
        },
        getSessionTimeout() {
            return !!this.sessionTimeout;
        },
        getLastUpdateTime() {
            return this.lastUpdateTime;
        },
        getTenant() {
            return this.tenantId || getAuthCache(TENANT_ID);
        },
    },
    actions: {
        setToken(info) {
            this.token = info ? info : ''; // for null or undefined value
            setAuthCache(TOKEN_KEY, info);
        },
        setRoleList(roleList) {
            this.roleList = roleList;
            setAuthCache(ROLES_KEY, roleList);
        },
        setUserInfo(info) {
            this.userInfo = info;
            this.lastUpdateTime = new Date().getTime();
            setAuthCache(USER_INFO_KEY, info);
        },
        setLoginInfo(info) {
            this.loginInfo = info;
            setAuthCache(LOGIN_INFO_KEY, info);
        },
        setAllDictItems(dictItems) {
            this.dictItems = dictItems;
            setAuthCache(DB_DICT_DATA_KEY, dictItems);
        },
        setTenant(id) {
            this.tenantId = id;
            setAuthCache(TENANT_ID, id);
        },
        setSessionTimeout(flag) {
            this.sessionTimeout = flag;
        },
        resetState() {
            this.userInfo = null;
            this.dictItems = [];
            this.token = '';
            this.roleList = [];
            this.sessionTimeout = false;
        },
        /**
         * 登录事件
         */
        async login(params) {
            try {
                const { goHome = true, mode, ...loginParams } = params;
                const data = await loginApi(loginParams, mode);
                const { token } = data;
                // save token
                this.setToken(token);
                return this.afterLoginAction(goHome, data);
            }
            catch (error) {
                return Promise.reject(error);
            }
        },
        /**
         * 扫码登录事件
         */
        async qrCodeLogin(token) {
            try {
                // save token
                this.setToken(token);
                return this.afterLoginAction(true, {});
            }
            catch (error) {
                return Promise.reject(error);
            }
        },
        /**
         * 登录完成处理
         * @param goHome
         */
        async afterLoginAction(goHome, data) {
            if (!this.getToken)
                return null;
            //获取用户信息
            const userInfo = await this.getUserInfoAction();
            const sessionTimeout = this.sessionTimeout;
            if (sessionTimeout) {
                this.setSessionTimeout(false);
            }
            else {
                const permissionStore = usePermissionStore();
                if (!permissionStore.isDynamicAddedRoute) {
                    const routes = await permissionStore.buildRoutesAction();
                    routes.forEach((route) => {
                        router.addRoute(route);
                    });
                    router.addRoute(PAGE_NOT_FOUND_ROUTE);
                    permissionStore.setDynamicAddedRoute(true);
                }
                await this.setLoginInfo({ ...data, isLogin: true });
                //update-begin-author:liusq date:2022-5-5 for:登录成功后缓存拖拽模块的接口前缀
                localStorage.setItem(JDragConfigEnum.DRAG_BASE_URL, useGlobSetting().domainUrl);
                //update-end-author:liusq date:2022-5-5 for: 登录成功后缓存拖拽模块的接口前缀
                goHome && (await router.replace((userInfo && userInfo.homePath) || PageEnum.BASE_HOME));
            }
            return data;
        },
        /**
         * 手机号登录
         * @param params
         */
        async phoneLogin(params) {
            try {
                const { goHome = true, mode, ...loginParams } = params;
                const data = await phoneLoginApi(loginParams, mode);
                const { token } = data;
                // save token
                this.setToken(token);
                return this.afterLoginAction(goHome, data);
            }
            catch (error) {
                return Promise.reject(error);
            }
        },
        /**
         * 获取用户信息
         */
        async getUserInfoAction() {
            if (!this.getToken) {
                return null;
            }
            // @ts-ignore
            const { userInfo, sysAllDictItems, roles } = await getUserInfo();
            if (userInfo) {
                // const { roles = [] } = userInfo; //从后台获取用户角色，此处隐藏
                // console.log("===从后台获取用户角色=====:",roles)
                if (isArray(roles)) {
                    const roleList = roles.map((item) => item.value);
                    this.setRoleList(roleList);
                }
                else {
                    userInfo.roles = [];
                    this.setRoleList([]);
                }
                this.setUserInfo(userInfo);
            }
            /**
             * 添加字典信息到缓存
             * @updateBy:lsq
             * @updateDate:2021-09-08
             */
            if (sysAllDictItems) {
                this.setAllDictItems(sysAllDictItems);
            }
            return userInfo;
        },
        /**
         * 退出登录
         */
        async logout(goLogin = false) {
            if (this.getToken) {
                try {
                    await doLogout();
                }
                catch {
                    console.log('注销Token失败');
                }
            }
            // //update-begin-author:taoyan date:2022-5-5 for: src/layouts/default/header/index.vue showLoginSelect方法 获取tenantId 退出登录后再次登录依然能获取到值，没有清空
            // let username:any = this.userInfo && this.userInfo.username;
            // if(username){
            //   removeAuthCache(username)
            // }
            // //update-end-author:taoyan date:2022-5-5 for: src/layouts/default/header/index.vue showLoginSelect方法 获取tenantId 退出登录后再次登录依然能获取到值，没有清空
            this.setToken('');
            setAuthCache(TOKEN_KEY, null);
            setAuthCache(TENANT_ID, 0);
            this.setSessionTimeout(false);
            this.setUserInfo(null);
            this.setLoginInfo(null);
            //update-begin-author:liusq date:2022-5-5 for:退出登录后清除拖拽模块的接口前缀
            localStorage.removeItem(JDragConfigEnum.DRAG_BASE_URL);
            //update-end-author:liusq date:2022-5-5 for: 退出登录后清除拖拽模块的接口前缀
            //如果开启单点登录,则跳转到单点统一登录中心
            const openSso = useGlobSetting().openSso;
            if (openSso == 'true') {
                await useSso().ssoLoginOut();
            }
            goLogin && (await router.push(PageEnum.BASE_LOGIN));
        },
        /**
         * 登录事件
         */
        async ThirdLogin(params) {
            try {
                const { goHome = true, mode, ...ThirdLoginParams } = params;
                const data = await thirdLogin(ThirdLoginParams, mode);
                const { token } = data;
                // save token
                this.setToken(token);
                return this.afterLoginAction(goHome, data);
            }
            catch (error) {
                return Promise.reject(error);
            }
        },
        /**
         * 退出询问
         */
        confirmLoginOut() {
            const { createConfirm } = useMessage();
            const { t } = useI18n();
            createConfirm({
                iconType: 'warning',
                title: t('sys.app.logoutTip'),
                content: t('sys.app.logoutMessage'),
                onOk: async () => {
                    await this.logout(true);
                },
            });
        },
    },
});
// Need to be used outside the setup
export function useUserStoreWithOut() {
    return useUserStore(store);
}
